package com.taotao.portal.controller;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.portal.service.ISearchService;

@Controller
public class SearchController {
	@Autowired
	private ISearchService searchService;

	@RequestMapping("/search")
	public String Search(@RequestParam(value = "q") String queryString,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "60") Integer rows, Model model) {
		if (StringUtils.isNotBlank(queryString)) {
			try {
				queryString = new String(queryString.getBytes("iso8859-1"),
						"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		TaotaoResult taotaoResult = this.searchService.Search(queryString,
				page, rows);
		SearchResult searchResult = (SearchResult) taotaoResult.getData();
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", searchResult.getPageCount());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("Page", page);
		return "search";
	}
}
